<template>
  <div class="slider" ref="sliderRef">
    <div class="slider-group">
      <div
        v-for="item of sliders"
        :key="item.id"
        class="slider-page"
      >
        <a :href="item.link">
          <img :src="item.pic" :alt="item.link" />
        </a>
      </div>
    </div>
    <div class="dots-wrapper">
      <span
        v-for="(item, index) in sliders"
        :key="item.id"
        class="dot"
        :class="{'active': currentPageIndex === index}"
      >
      </span>
    </div>
  </div>
</template>

<script>
  import { defineComponent, ref } from 'vue'
  import useSlider from './use-slider'

  export default defineComponent({
    name: 'slider',
    props: {
      sliders: {
        type: Array,
        default: () => []
      }
    },
    setup() {
      const sliderRef = ref(null)
      const { currentPageIndex } = useSlider(sliderRef)
      return {
        sliderRef,
        currentPageIndex
      }
    }
  })
</script>

<style lang="scss" scoped>
  .slider {
    min-height: 1px;
    font-size: 0;
    touch-action: pan-y;
    .slider-group {
      position: relative;
      overflow: hidden;
      white-space: nowrap;
      .slider-page {
        display: inline-block;
        transform: translate3d(0, 0, 0);
        backface-visibility: hidden;
        a {
          display: block;
          width: 100%;
        }
        img {
          display: block;
          width: 100%;
        }
      }
    }
    .dots-wrapper {
      position: absolute;
      left: 50%;
      bottom: 12px;
      line-height: 12px;
      transform: translateX(-50%);
      .dot {
        display: inline-block;
        margin: 0 4px;
        width: 8px;
        height: 8px;
        border-radius: 50%;
        background: $color-text-l;
        &.active {
          width: 20px;
          border-radius: 5px;
          background: $color-text-ll;
        }
      }
    }
  }
</style>
